package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfStatusEnum;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCWfEmpVw;
import com.auxgroup.hr.front.dao.domain.PsCWfWkotDtl;
import com.auxgroup.hr.front.dao.domain.PsCWfWkotHdr;
import com.auxgroup.hr.front.dao.repository.PsCWfEmpVwRepository;
import com.auxgroup.hr.front.service.process.WfWkotHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfWkotRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 加班审批
 * @author nitongqian
 * @version 创建时间：190303
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfwkothdr")
public class WfWkotHdrCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfWkotHdrCO.class);

	@Autowired
	public WfWkotHdrService  wfWkotHdrService;

	@Autowired
	private PsCWfEmpVwRepository psCWfEmpVwRepository;
	

	@GetMapping(value="/getWfWkotHdr")
	public BaseResponseVO getWfWkotHdr(String cTransactionNbr,@SessionGain("user") UserDTO user){
		if(StringUtils.isBlank(cTransactionNbr)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		PsCWfWkotHdr wkotHdr =  wfWkotHdrService.getWfInfo(cTransactionNbr);
		if(wkotHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}

		WfWkotRespVO respVO = objectMapper.map(wkotHdr, WfWkotRespVO.class);
		List<PsCWfWkotDtl> dtls = wfWkotHdrService.queryByNbr(cTransactionNbr);
		respVO.setAppliers(
				dtls.stream().map(dtl-> {
							JobCurrPrimary primary = new JobCurrPrimary();
							primary.setEmplid(dtl.getEmplid());
					primary.setEmplRcd(new BigDecimal(0));
							PsCWfEmpVw vw = psCWfEmpVwRepository.findOne(primary);
							WfWkotRespVO.WkotDtlVO vo = objectMapper.map(dtl, WfWkotRespVO.WkotDtlVO.class);
							vo.setName(vw.getNameDisplay());
							vo.setDeptid(vw.getDeptDescr());
							return vo;
						}
				).collect(Collectors.toList()));
		//加班事由
		fillBaseWfInfo(user, wkotHdr, respVO);
		BaseResponseVO r = buildRespone(respVO);
		return buildRespone(respVO);
	}

	/**
	 * 创建加班流程
	 * @param user
	 * @return
	 */
	@PostMapping(value="/saveOvrt")
	public BaseResponseVO saveWfWkotHdr(@RequestBody PsCWfWkotHdr req, @SessionGain("user") UserDTO user){
		if (!fillBeforeSubmit(req, user)) return buildRespone(BaseRetCode.CODE_ERROR_GET_TRANSACTION_NBR_FAILED);

		ObjectFieldUtils.handleNullValue(req);
        PsCWfWkotHdr wkotHdr = wfWkotHdrService.saveWfInfo(req, user);
        if(wkotHdr != null){
			BaseResponseVO resp = submitWf(user, WfTypeEnum.WF_STYPE_OVRT.getType(), wkotHdr);
			//提交失败  并且是首次提交的
			if(!BaseRetCode.CODE_SUCCESS.getRetCode().equals(resp.getRetCode())
			 	&& WfStatusEnum.WF_STATUS_S.getStatus().equals(wkotHdr.getcWfStatus())){
				//流程发起失败，删除数据
//				wfWkotHdrService.delWfInfo(req.getcTransactionNbr());
				logger.error("加班流程申请失败");
			}
			return resp;
        }else{
        	return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_APPLY_FAIL);
        }
	}



	@Override
	protected Set<String> getAuthFileds() {
		return null;
	}
}
